﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManYan.Common.Middleware
{
    public class StopWatchMiddleware
    {
        private readonly RequestDelegate? requestDelegate;

        public StopWatchMiddleware(RequestDelegate? requestDelegate)
        {
            this.requestDelegate = requestDelegate;
        }

        public async Task Invoke(HttpContext context)
        {
            if (requestDelegate != null)
            {
                Stopwatch stopwatch = new Stopwatch();//开始记数
                stopwatch.Start();//开始
                await requestDelegate.Invoke(context);//调用方法
                stopwatch.Stop();//停止记数
                Console.WriteLine(stopwatch.ElapsedMilliseconds);//输出这个方法的执行时间
            }
        }
    }
}
